extends Camera2D

@export var enable_smoothing: bool = true
@export var smoothing_speed: float = 6.0
@export var snap_to_pixels: bool = true

var player: Node2D

func _ready():
	# 找到玩家并设为当前相机
	player = get_tree().get_first_node_in_group("player")
	make_current()

func _physics_process(delta: float) -> void:
	# 在物理帧里跟随，避免与玩家物理更新不同步造成抖动/重影
	if not player:
		player = get_tree().get_first_node_in_group("player")
		return
	var target: Vector2 = player.global_position
	if enable_smoothing:
		global_position = global_position.lerp(target, clamp(smoothing_speed * delta, 0.0, 1.0))
	else:
		global_position = target
	if snap_to_pixels:
		global_position = Vector2(round(global_position.x), round(global_position.y))


